package com.zhq.blog.manage.domain.controller.product;


import java.io.UnsupportedEncodingException;
import java.util.Base64;

import javax.validation.Valid;

import net.sf.json.JSONObject;

import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.zhq.blog.core.domain.product.Prize;
import com.zhq.blog.core.domain.search.PrizeSearch;
import com.zhq.blog.manage.domain.controller.BaseController;
import com.zhq.blog.manage.domain.controller.BossModel;

@Controller
@Scope("prototype")
@RequestMapping("/admin/product/prize/*")
public class PrizeController extends BaseController {
	
	/*@RequestMapping()
    public BossModel index(@PageableDefault(page = 0,size = DEFAULT_PAGE_SIZE,sort = "id",direction = Sort.Direction.DESC) Pageable pageable){
		return createModel("admin/product/prize/list").add("prizesPage",context.getPrizeRepository().findAll(pageable));
	}*/
	
	@RequestMapping()
    public BossModel index(String viewState,@PageableDefault(page = 0,size = DEFAULT_PAGE_SIZE,sort = "id",direction = Sort.Direction.DESC) Pageable pageable) throws UnsupportedEncodingException{
		PrizeSearch search = null;
		if (viewState != null && !viewState.trim().equals("")) {
			search = (PrizeSearch) JSONObject.toBean(JSONObject.fromObject(new String(Base64.getDecoder().decode(viewState), "UTF-8")), PrizeSearch.class);
			return createModel("admin/product/prize/list")
					.add("prizeSearch", search)
					.add("viewState", viewState)
					.add("categorys", context.getCategoryRepository().findAll())
					.add("prizesPage",context.getPrizeRepository().findAll(new Prize().selectCondition(search),pageable));
		} else {
			search = new PrizeSearch();
			return createModel("admin/product/prize/list").add("prizeSearch", search).add("categorys", context.getCategoryRepository().findAll()).add("viewState", "").add("prizesPage", context.getPrizeRepository().findAll(new Prize().selectCondition(search),pageable));

		}
	}
	
	@RequestMapping(value = "index", method = RequestMethod.POST)
	public BossModel index(PrizeSearch search,@PageableDefault(page = 0, size = DEFAULT_PAGE_SIZE, sort = "createdDate", direction = Sort.Direction.DESC) Pageable pageable)throws UnsupportedEncodingException {
		return createModel("admin/product/prize/list")
				.add("prizesPage", context.getPrizeRepository().findAll(new Prize().selectCondition(search),pageable))
				.add("viewState",Base64.getEncoder().encodeToString(JSONObject.fromObject(search).toString().getBytes("UTF-8"))).add("categorys", context.getCategoryRepository().findAll());
	}
	
	@RequestMapping(value="edit/{id}")
	public BossModel edit(@PathVariable long id){
		return createPrizeModel(context.getPrizeRepository().findOne(id));
	}
	
	@RequestMapping(value="add")
	public BossModel add(){
		return createPrizeModel(new Prize());
	}

	
	@RequestMapping(value="save",method = RequestMethod.POST)
	public BossModel save(@Valid final Prize prize, final BindingResult bindingResult, final ModelMap model, RedirectAttributes redirectPrizes,@PageableDefault(page = 0,size = DEFAULT_PAGE_SIZE) Pageable pageable){
		if (bindingResult.hasErrors()) {
			return createModel("admin/product/prize/info");
		}
		prize.init();
		context.getPrizeRepository().save(prize);
		String message = prize.getSucessFlashMessage();
		redirectPrizes.addFlashAttribute("message", message);
		return createModel("redirect:/admin/product/prize/index");
		
	}
	
	@RequestMapping(value="delete",method= RequestMethod.POST)
	public BossModel delete(long id,@PageableDefault(page = 0,size = DEFAULT_PAGE_SIZE,sort = "id",direction = Sort.Direction.DESC) Pageable pageable){
		context.getPrizeRepository().delete(id);
		return createModel("admin/product/prize/prize_table").add("prizesPage",context.getPrizeRepository().findAll(pageable));
	}

	private BossModel createPrizeModel(Prize prize) {
		BossModel modelAndView = createModel("admin/product/prize/info")
				.add("categorys", context.getCategoryRepository().findAll())
				.add("cards", context.getCardRepository().findAll());
		if (prize != null)
			modelAndView.add("prize", prize);
		return modelAndView;
	}

}
